package com.itheima.d2_buffered_stream;

import java.io.*;

/**
 * 目标：观察原始流和缓冲流的性能。
 *
 * 缓冲流字节数组拷贝 >  基本流字节数组拷贝 > 缓冲流单字节拷贝 > 基本流单字节拷贝
 */
public class Test4Time {
    // 复制的视频路径  TODO 设置一个大文件测试
    private static final String SRC_FILE = "day10io-app2/file/video1.mp4";
    // 复制到哪个目的地
    private static final String DEST_FILE = "day10io-app2/file/";

   final static int bufsize = 1024 * 2;


    public static void main(String[] args) {
//         copy01(); // 低级字节流一个一个字节的赋值，慢的简直让人无法忍受，直接淘汰！   未知
//         copy02();// 低级的字节流流按照一个一个字节数组的形式复制，   0.542s
//         copy03(); // 缓冲流按照一个一个字节的形式复制，速度较慢,直接淘汰！   2.442s
         copy04(); // 缓冲流按照一个一个字节数组的形式复制，   0.498s
    }

    //基本字节流读写单个字节，复制
    private static void copy01() {
        long startTime = System.currentTimeMillis();
        try (
                InputStream is = new FileInputStream(SRC_FILE);
                OutputStream os = new FileOutputStream(DEST_FILE + "video-cp1.mp4");
        ) {

            int b;
            while ((b = is.read()) != -1) {
                os.write(b);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("低级字节流一个一个字节复制耗时：" + (endTime - startTime) / 1000.0 + "s");
    }

    private static void copy02() {
        long startTime = System.currentTimeMillis();
        try (
                InputStream is = new FileInputStream(SRC_FILE);
                OutputStream os = new FileOutputStream(DEST_FILE + "video-cp2.mp4");
        ) {
            byte[] buffer = new byte[bufsize];
            int len;
            while ((len = is.read(buffer)) != -1) {
                os.write(buffer, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("低级字节流使用字节数组复制耗时：" + (endTime - startTime) / 1000.0 + "s");
    }

    private static void copy03() {
        long startTime = System.currentTimeMillis();
        try (
                InputStream is = new FileInputStream(SRC_FILE);
                BufferedInputStream bis = new BufferedInputStream(is);
                OutputStream os = new FileOutputStream(DEST_FILE + "video-cp3.mp4");
                BufferedOutputStream bos = new BufferedOutputStream(os);
        ) {

            int b;
            while ((b = bis.read()) != -1) {
                bos.write(b);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("缓冲流一个一个字节复制耗时：" + (endTime - startTime) / 1000.0 + "s");
    }

    private static void copy04() {
        long startTime = System.currentTimeMillis();
        try (
                InputStream is = new FileInputStream(SRC_FILE);
                BufferedInputStream bis = new BufferedInputStream(is, bufsize);
                OutputStream os = new FileOutputStream(DEST_FILE + "video-cp4.mp4");
                BufferedOutputStream bos = new BufferedOutputStream(os, bufsize);
        ) {
            byte[] buffer = new byte[bufsize]; // 32KB
            int len;
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("缓冲流使用字节数组复制耗时：" + (endTime - startTime) / 1000.0 + "s");
    }
}










